package com.wrater.zero.common.global;

import com.wrater.zero.common.base.BaseException;
import com.wrater.zero.common.base.ResultExceptionEnum;
import com.wrater.zero.common.base.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandling {


    /**
     * 自定义异常
     */
    @ExceptionHandler(value = BaseException.class)
    public ResultVo processException(BaseException e) {
        log.error("位置:{} -> 错误信息:{}", e.getAddress() ,e.getLocalizedMessage());
        return ResultVo.error(e);
    }

    /**
     * 通用异常
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(Exception.class)
    public ResultVo exception(Exception e) {
        e.printStackTrace();
        return ResultVo.error(new BaseException(ResultExceptionEnum.UNKNOWN_EXCEPTION, ResultVo.class.toString()));
    }
}